{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 线性代数\n",
    "import numpy as np\n",
    "from numpy.linalg import inv, qr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  28.,   64.],\n",
       "       [  67.,  181.]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.array([[1., 2., 3.], [4., 5., 6.]])\n",
    "y = np.array([[6., 23.], [-1, 7], [8, 9]])\n",
    "x.dot(y) # 矩阵乘法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  6.,  15.])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.dot(x, np.ones(3)) # 2*3矩阵乘以一个3*1矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.99946364  0.42488643 -0.53900775 -0.9701616   0.45676171]\n",
      " [ 0.42488643  0.45529242 -0.32430143 -0.6244948   0.26716755]\n",
      " [-0.53900775 -0.32430143  2.6797144   0.56083036 -2.4860754 ]\n",
      " [-0.9701616  -0.6244948   0.56083036  1.57454682 -0.45628419]\n",
      " [ 0.45676171  0.26716755 -2.4860754  -0.45628419  2.58170138]]\n",
      "[[  1.00000000e+00  -4.37715753e-17   2.29547615e-16  -8.81655164e-17\n",
      "    1.73487789e-17]\n",
      " [ -2.63382816e-16   1.00000000e+00   2.91494870e-16  -4.23624252e-17\n",
      "   -1.13579522e-16]\n",
      " [  2.61904855e-17   8.32944592e-17   1.00000000e+00  -2.43846592e-16\n",
      "    1.58904870e-15]\n",
      " [ -9.69321863e-17  -1.13633466e-17   4.75161964e-16   1.00000000e+00\n",
      "   -6.22206142e-17]\n",
      " [ -2.36280139e-18   5.65816389e-17  -3.07226737e-16   1.08127679e-16\n",
      "    1.00000000e+00]]\n"
     ]
    }
   ],
   "source": [
    "x = np.random.randn(5, 5)\n",
    "mat = x.T.dot(x)\n",
    "mat_i = inv(mat)\n",
    "print(mat_i)\n",
    "print(mat.dot(mat_i)) # 单位矩阵，因为浮点数，所以非对角线元素为非0的非常小的数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.87346405,  0.03568355,  0.03011668,  0.4682239 ,  0.12508642],\n",
       "       [ 0.13746522, -0.93839611,  0.04066364,  0.30579304,  0.07316513],\n",
       "       [-0.07502753, -0.06490287, -0.71969369,  0.09315781, -0.68082384],\n",
       "       [-0.46082062, -0.33609472,  0.09215151, -0.8065847 , -0.12495564],\n",
       "       [-0.01338287, -0.03110275, -0.68628605, -0.16733122,  0.70701148]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "q, r = qr(mat) # qr分解\n",
    "q"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 常用线性代数函数\n",
    "# diag： 以一维数组形式返回方阵的对角线（或非对角线）元素，\n",
    "#        或将一维数组转换为方阵。\n",
    "# dot：  矩阵乘法\n",
    "# trace：计算对角线元素的和\n",
    "# det：  计算矩阵行列式\n",
    "# eig：  计算方阵的本征值和本征向量\n",
    "# inv：  计算方阵的逆\n",
    "# pinv： 就按矩阵的Moore-Penrose伪逆\n",
    "# qr：   计算QR分解\n",
    "# svd：  计算奇异值分解（SVD）\n",
    "# solve：解线性方程组Ax = b，其中A为一个方阵。\n",
    "# lstsq：计算Ax = b的最小二乘解"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
